package com.sinosoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.report.Item;
import com.sinosoft.report.ReportComments;
import com.sinosoft.report.ReportManager;
import com.sinosoft.report.ReportView;
import com.sinosoft.util.CheckEmpty;
import com.sinosoft.util.PropertiesUtil;
import com.sinosoft.util.SystemConfigUtil;

@SuppressWarnings("unchecked")
@Controller
public class DownloadReportController extends BaseFormController {
	
	private ReportManager reportMan;

	@Autowired
	public void setReportManager(ReportManager reportMan) {
		this.reportMan = reportMan;
	}
	
	@RequestMapping("report/downloadreport.html")
	protected ModelAndView downlaodreport(ReportView vo,Model model,HttpServletRequest request,
			HttpServletResponse response) {
		String group = ReportComments.countStr(vo.getGroup(), ",");
		String order = ReportComments.filterOrder(vo.getGroup(), vo.getOrder(), ",");
		vo.setStrgroup(group);
		vo.setStrorder(order);
		List list = reportMan.searchDownloadSize(vo);
		model.addAttribute("reportview", vo);
		if(null!=list&&list.size()>0){
			model.addAttribute("listresult", true);
		}else{
			model.addAttribute("listresult", false);
		}
		//产品级别
		String[] productlevels = PropertiesUtil.getPropertiesValue("shopcar.productlevels", request);
		List<Item> productlevelsmap =ReportComments.getList(productlevels);
		//应用领域
		String[] territory = PropertiesUtil.getPropertiesValue("territory", request);
		List<Item> territorymap =ReportComments.getList(territory);
		//用户等级
		String[] usergrade = PropertiesUtil.getPropertiesValue("count.grade.info", request);
		List<Item> usergrademap =ReportComments.getList(usergrade);
		//卫星传感器
		if(!CheckEmpty.isEmpty(vo.getSatellte())){
			String[] senor = SystemConfigUtil.getSystemValue(vo.getSatellte(),
					getServletContext());
			model.addAttribute("senor", senor);
		}
		
		model.addAttribute("territorymap", territorymap);
		model.addAttribute("productlevelsmap", productlevelsmap);
		model.addAttribute("usergrademap", usergrademap);
		// 卫星
		String[] satellite = SystemConfigUtil.getSystemValue("satellite", this
				.getServletContext());
		model.addAttribute("satellite", satellite);
		return new ModelAndView("report/downloadreport", "downloadlist",list);
	}
	
	/**
	 * 采集单图形统计
	 * @param starttime
	 * @param endtime
	 * @param stry
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("report/downloadpicreport.html")
	protected ModelAndView countDownlaodSize(String starttime, String endtime,
			String stry, HttpServletRequest request,
			HttpServletResponse response) {
		String group = stry;
		if (CheckEmpty.isEmpty(stry) || stry.equals("null") || stry.equals("")) {
			group = "d.productlevel";
		}

		List<ReportView> list = reportMan.searchDownloadSize(group, starttime,
				endtime);
		Map<String, Integer> map = new HashMap<String, Integer>();
		int count=0;
		for (int i = 0; i < list.size(); i++) {
			ReportView vo = list.get(i);
			count+=vo.getNum();
			map.put(vo.getValue(), vo.getNum());
		}

		JSONObject jsonobj = new JSONObject();
		jsonobj.put("ingredients", map);
		jsonobj.put("count", count);

		try {
			response.getWriter().write(jsonobj.toString());
		} catch (Exception e) {

		}
		return null;
	}
}
